package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.*;
import com.jyy.prefabricated.enums.ExerciseStatusEnum;
import com.jyy.prefabricated.enums.QuestionBankTypeEnum;
import com.jyy.prefabricated.mapper.*;
import com.jyy.prefabricated.vo.QuestionBankVo;
import com.jyy.prefabricated.vo.StudentExerciseQuestionVo;
import com.jyy.prefabricated.vo.StudentQuestionBankNoteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentQuestionBankService {

    @Autowired
    private QuestionBankCategoryService questionBankCategoryService;

    @Autowired
    private QuestionBankMapper questionBankMapper;

    @Autowired
    private StudentExerciseMapper studentExerciseMapper;

    @Autowired
    private StudentExerciseQuestionMapper studentExerciseQuestionMapper;

    @Autowired
    private StudentErrorQuestionMapper studentErrorQuestionMapper;

    /**
     * 获取学员被授权的所有题库列表
     * @param name
     * @param studentId
     * @return
     */
    public List<QuestionBankVo> selectQuestionBankByStudent(String name, Integer studentId, List<Integer> categoryIds){
        return questionBankMapper.selectByStudent(name,studentId, QuestionBankTypeEnum.EXERCISE.getValue(),categoryIds);
    }

    /**
     * 获取学员错题题库列表
     * @param name
     * @param studentId
     * @return
     */
    public List<QuestionBankVo> selectErrorQuestionBankByStudent(String name, Integer studentId, List<Integer> categoryIds){
        return questionBankMapper.selectErrorByStudent(name,studentId, QuestionBankTypeEnum.EXERCISE.getValue(),categoryIds);
    }

    /**
     * 获取收藏的所有题库列表
     * @param name
     * @param studentId
     * @return
     */
    public List<QuestionBankVo> selectCollectionQuestionBankByStudent(String name, Integer studentId, List<Integer> categoryIds){
        return questionBankMapper.selectCollectionByStudent(name,studentId, QuestionBankTypeEnum.EXERCISE.getValue(),categoryIds);
    }

    /**
     * 获取记录笔记的所有题库列表
     * @param name
     * @param studentId
     * @return
     */
    public List<QuestionBankVo> selectNoteQuestionBankByStudent(String name, Integer studentId, List<Integer> categoryIds){
        return questionBankMapper.selectNoteByStudent(name,studentId, QuestionBankTypeEnum.EXERCISE.getValue(),categoryIds);
    }

    /**
     * 根据题库id获取学员笔记
     * @param questionBankId
     * @return
     */
    public StudentQuestionBankNoteVo fetchQuestionNotesByQuestionBankId(Integer questionBankId,Integer studentId){
        StudentQuestionBankNoteVo questionBankNoteVo = new StudentQuestionBankNoteVo();
        QuestionBank questionBank = questionBankMapper.selectByPrimaryKey(questionBankId);
        List<StudentExercise> exercises = studentExerciseMapper.selectByQuestionIdAndStudentId(questionBankId,studentId);
        questionBankNoteVo.setTitle(questionBank.getName());
        //查询最近一次完成的练习
        List<StudentExercise> filterExercises = exercises.stream().filter(e->ExerciseStatusEnum.AFTER.getValue().equals(e.getExerciseStatus())).collect(Collectors.toList());
        if(filterExercises.size() > 0){
            List<StudentExerciseQuestionVo> questions = studentExerciseQuestionMapper.selectQuestionVoByStudentExerciseId(filterExercises.get(0).getId());
            List<StudentExerciseQuestionVo> filterQuestions = questions.stream().filter(e->e.getNote() != null).collect(Collectors.toList());
            questionBankNoteVo.setQuestionCount(filterQuestions.size());
            questionBankNoteVo.setQuestions(filterQuestions);
        }
        return questionBankNoteVo;
    }

    /**
     * 根据题库id获取学员错题
     * @param questionBankId
     * @return
     */
    public StudentQuestionBankNoteVo fetchErrorQuestionsByQuestionBankId(Integer questionBankId,Integer studentId){
        StudentQuestionBankNoteVo questionBankNoteVo = new StudentQuestionBankNoteVo();
        QuestionBank questionBank = questionBankMapper.selectByPrimaryKey(questionBankId);
        List<StudentExerciseQuestionVo> questions = studentErrorQuestionMapper.selectQuestionVoByQuestionBankIdAndStudentId(questionBankId,studentId);
        questionBankNoteVo.setTitle(questionBank.getName());
        questionBankNoteVo.setQuestionCount(questions.size());
        questionBankNoteVo.setQuestions(questions);

        return questionBankNoteVo;
    }

    /**
     * 删除学员错题本题库
     * @param questionBankId
     * @param studentId
     */
    public void deleteErrorQuestionBank(Integer questionBankId,Integer studentId){
        studentErrorQuestionMapper.deleteByQuestionBankIdAndStudentId(questionBankId,studentId);
    }

}
